import java.sql.Connection;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;

public class DepositCheckThread extends MyThread implements Runnable {

	private boolean stopFlag; // flag for stop thread(true - stop,false - run)
	private String threadName; // name of thread

	public DepositCheckThread(Connection initConnection, DepositDBManager initDepositDBManager) {

		super(initConnection, initDepositDBManager);
		// TODO Auto-generated constructor stub
		setStopFlag(false);
		setThreadName("DepositCheckThread");

	}

	public boolean isStopFlag() {
		return stopFlag;
	}

	public void setStopFlag(boolean stopFlag) {
		this.stopFlag = stopFlag;
	}

	public String getThreadName() {
		return threadName;
	}

	public void setThreadName(String name) {
		this.threadName = name;
	}

	// --------------------------------------------------------------------------------
	public void run() {

		while (!stopFlag) { // while thread running

			// Process run one time in day
			// Check if deposit (ending date == current date)
			// We call function delete Deposit ( which update Bank balance and
			// delete appropriate deposit )

			ResultSet rset = null;
			Statement statement = null;
			DepositDBManager depositDBManager = super.getDepositDBManager();
			System.out.println("[" + getThreadName() + "] :" + "thread is running ");

			// receive currDate ( current data )
			Calendar today = Calendar.getInstance();
			today.set(Calendar.HOUR, 0); // today should not contains
			today.set(Calendar.MINUTE, 0); // hour:minute:seconds
			today.set(Calendar.SECOND, 0);
			Date currDate = new Date(today.getTimeInMillis());

			// convert currDate to appropriate string
			String currDateString = currDate.toString();

			Connection conn = getConnection(); // receive connection

			try {

				statement = conn.createStatement();
				String query = ("SELECT  deposit_id FROM deposits ");
				rset = statement.executeQuery(query);

				while (rset.next() == true) {

					Long depositId = rset.getLong(1);
					Deposit deposit = depositDBManager.getDepositById(depositId);

					// receive ending date and convert it to appropriate string
					Date endingDate = deposit.getEndingDate();
					String endingDateString = endingDate.toString();

					// if CurrentDate == Ending Date for Deposit
					// Call to DeleteDepositFunction
					if (currDateString.equals(endingDateString)) {
						System.out.println("[ " + getThreadName() + " ] For deposit_id = " + deposit.getDepositId() + ", time is over ");
						depositDBManager.deleteDeposit(deposit.getDepositId());
					}

				}
			} catch (SQLException e) {
				e.printStackTrace();
			} catch (DBManagerException e) {
				e.printStackTrace();
			}

			// sleep to 1 month ( 1 sec = 1000 millisec ,
			// 24 hours = 8640000 , 1 month = 259200000 )
			try {
				Thread.sleep(3000); // sleep 1 day == 3 sec for our testing
			} catch (Exception e) {
				// TODO: handle exception
			}

		} // while
	}

	// --------------------------------------------------------------------------------
	public void stopRunning() {
		// stop thread running
		setStopFlag(true);
		System.out.println("[ " + getThreadName() + " ]: " + " thread stop running ");
	}
	// ------------------------------------------------------------------------------
}